package com.shigu.user.action;

import com.alibaba.fastjson.JSONObject;
import com.shigu.exceptions.WapException;
import com.shigu.user.bo.WxCodeRequest;
import com.shigu.user.bo.WxLoginRequest;
import com.shigu.user.bo.WxEntryRequest;
import com.shigu.user.service.AppLoginService;
import com.shigu.user.vo.UserOpenIdResponse;
import com.shigu.user.vo.UserPhoneResponse;
import com.shigu.weixin.service.WxUserService;
import com.shigu.weixin.util.WxEntryDataUtil;
import com.shigu.user.vo.XcxLoginResponse;
import com.shigu.weixin.vo.XcxAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/wapsjson")
public class WxController {
    @Autowired
    WxUserService wxUserService;
    @Autowired
    AppLoginService appLoginService;
    /**
     * @description 微信小程序 获取到用户授权 信息后送后台进行进行登录
     * @author 郭尔
     * @date 2019/1/21 14:53
     */
    @RequestMapping("getWxLoginUser")
    @ResponseBody
    public XcxLoginResponse getWxLoginUser(@Valid WxLoginRequest request, BindingResult result) throws Exception {
        if(result.hasErrors()){
            throw new WapException(result.getFieldError().getDefaultMessage());
        }

        String dataJson = WxEntryDataUtil.getDataJson(request.getEncrypdata(), request.getIvdata(), request.getSessionkey());
        JSONObject object = JSONObject.parseObject(dataJson);

        return appLoginService.getWxLoginUser(request.getOpenid(),request.getHeadUrl(),request.getNick(),object.getString("unionId"));
    }

    /**
     * @description 微信code换取openid 和session_key
     * @author 郭尔
     * @date 2019/1/23 13:21
     */
    @RequestMapping("exchangeOpenId")
    @ResponseBody
    public UserOpenIdResponse exchangeOpenId(@Valid WxCodeRequest request,BindingResult result) throws Exception {
        if(result.hasErrors()){
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        XcxAuthVo vo = wxUserService.xcxAuth(request.getCode());
        UserOpenIdResponse response = new UserOpenIdResponse();
        response.setOpenId(vo.getOpenid());
        response.setSessionKey(vo.getSessionKey());
        return response.success();
    }

    /**
     * @description 获取用户手机号
     * @author 郭尔
     * @date 2019/2/13 10:38
     */
    @RequestMapping("userPhone")
    @ResponseBody
    public UserPhoneResponse userPhone(WxEntryRequest request) throws Exception {
        String json = WxEntryDataUtil.getDataJson(request.getEncrypdata(), request.getIvdata(), request.getSessionkey());
        JSONObject object = JSONObject.parseObject(json);
        UserPhoneResponse response = new UserPhoneResponse();
        response.setPhoneNumber(object.getString("phoneNumber"));
        return response.success();
    }



}
